﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CmpClass;

namespace CmpEdu
{
    public partial class FormTest5_4 : Form
    {
        public FormTest5_4()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Class-Form1中定义Student，窗口出现初始化
        /// </summary>
        Student[] stu = new Student[3];
        //Student stu = new Student();
        private void FormTest5_4_Load(object sender, EventArgs e)
        {
            this.Text = "结构数组应用示例";
            //tableLayoutPanel1.Visible = false;
            RadioButtonName.Checked = false;
            RadioButtonNo.Checked = true;

            //初始化Student结构体            
            stu[0].StuNo = "41163009"; stu[0].StuName = "李团长"; stu[0].StuClass = "信计1101";
            stu[0].StuMath = 85; stu[0].StuChn = 80; stu[0].StuEng = 90;
            stu[1].StuNo = "41163007"; stu[1].StuName = "鸿飞"; stu[1].StuClass = "信计1101";
            stu[1].StuMath = 85; stu[1].StuChn = 80; stu[1].StuEng = 90;
            stu[2].StuNo = "41163011"; stu[2].StuName = "ac"; stu[2].StuClass = "信计1101";
            stu[2].StuMath = 85; stu[2].StuChn = 80; stu[2].StuEng = 90;
        }       

        /// <summary>
        /// 按照查询条件查询学生记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnQuery_Click(object sender, EventArgs e)
        {
            ClearStuInfo();
            if (InputInfo.Text == "")
            {
                MessageBox.Show("你还没有输入信息呢！！！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int CheckSelect = 0; //默认是选择学号查询
            if (RadioButtonName.Checked)
            {
                CheckSelect = 1;
            }
            if (!CheckStuArray(InputInfo.Text, CheckSelect))
            {
                //tableLayoutPanel1.Visible = false;
                MessageBox.Show("未找到该学生记录", "请检查条件", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 给几个label赋值
        /// </summary>
        /// <param name="StuIndex"></param>
        public void ShowStuInfo(int StuIndex)
        {
            LabelNo.Text = stu[StuIndex].StuNo;
            LabelName.Text = stu[StuIndex].StuName;
            LabelClass.Text = stu[StuIndex].StuClass;
            LabelMath.Text = stu[StuIndex].StuMath.ToString();
            LabelChn.Text = stu[StuIndex].StuChn.ToString();
            LabelEng.Text = stu[StuIndex].StuEng.ToString();
        }

        /// <summary>
        /// 清空几个label的值,初始化
        /// </summary>
        /// <param name="StuIndex"></param>
        public void ClearStuInfo()
        {
            LabelNo.Text = "";
            LabelName.Text = "";
            LabelClass.Text = "";
            LabelMath.Text = "";
            LabelChn.Text = "";
            LabelEng.Text = "";
        }

        /// <summary>
        /// 查询是否存在这条学生记录，如果存在，则显示该学生信息
        /// </summary>
        /// <param name="QueryType"></param>
        /// <param name="CheckSelect"></param>
        /// <returns></returns>
        public bool CheckStuArray(string InputInfo, int CheckSelect)
        {
            bool IsExist = false;
            int i = 0;
            if (CheckSelect == 0) //学号查询
            {
                foreach (Student s in stu)
                {
                    if (s.StuNo == InputInfo)
                    {
                        IsExist = true;
                        ShowStuInfo(i);
                    }
                    i++;
                }
            }
            else //姓名查询
            {
                foreach (Student s in stu)
                {
                    if (s.StuName == InputInfo)
                    {
                        IsExist = true;
                        ShowStuInfo(i);
                    }
                    i++;
                }
            }
            return IsExist;
        }
    }
}
